<?php

namespace App\Process;

use App\Core\BaseModel;
use App\Event\Event;
use App\Model\Setting\JobModel;
use App\Model\Setting\JobResultModel;
use EasySwoole\Component\Singleton;
use EasySwoole\Queue\Queue;

class LmtQueue extends Queue
{
    use Singleton;

    public function responseExecutionResult($job){
        $jobData = $job->getJobData();

        // 任务id
        $job_id = md5(json_encode($jobData));

        /**判断该任务是否已经执行完成*/
        if(JobModel::getOne(['job_id' => $job_id, 'job_result' => 1])){
            return true;
        }

        $result = Event::getInstance()->hook($jobData['event'], $jobData['args']);

        /**记录任务执行信息*/
        $jobrecordData = [
            'job_id' => md5(json_encode($jobData)),
            'job_result' => $result['result'] ? BaseModel::ENABLED : BaseModel::DISABLED,
            'job_message' => $result['message'] ?? ''
        ];
        if($result['result']){
            $this->consumer()->confirm($job);
            /**更新任务表执行结果*/
            JobModel::updateData(['job_result' => 1], ['job_id' => $jobrecordData['job_id']]);
        }
        JobResultModel::insertData($jobrecordData);

    }
}